home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10185 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!ind-003-236-135
  3. From: dlmiller@iquest.net (Doug Miller)
  4. Subject: Re: while loop problem
  5. X-Nntp-Posting-Host: ind-003-236-135.iquest.net
  6. Message-ID: <DoBAC8.K3o@iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Network Services
  9. X-Newsreader: News Xpress Version 1.0 Beta #2.1
  10. References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca> <Do6DB0.EtE@microunity.com> <Pine.A32.3.91.960312171258.149477C-100000@black.weeg.uiowa.edu>
  11. Date: Fri, 15 Mar 1996 13:50:21 GMT
  12.  
  13. The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
  14.  
  15. +On Tue, 12 Mar 1996, Tom Sanders wrote:
  16. +
  17. +> Bill Simpson <wsimpson@uwinnipeg.ca> writes:
  18. +> |> Consider the following code fragment:
  19. +> |>
  20. +> |> y=2000; z=1000;
  21. +> |> x=0;
  22. +> |> while(x<=XMAX)
  23. +> |>     {
  24. +> |>     x+=(int) erand(mean);
  25. +> |>     setdot(x,y,z);
  26. +> |>     }
  27. +> |>
  28. +> |> The above code will also attempt to plot one point at an x value >XMAX
  29. +> |> before it terminates.
  30. +> |>
  31. +> |> Is there a nice way to write the code so it works properly?
  32. +>
  33. +> y=2000; z=1000;
  34. +> x=0;
  35. +> do  {
  36. +>      x+=(int) erand(mean);
  37. +>      setdot(x,y,z);
  38. +>      } while(x<=XMAX)
  39. +
  40. +  This has exactly the same problem as the original loop, because it adds
  41. +erand() to x and calls setdot() _before_ checking x's value against XMAX.
  42. +Also, you left off the semicolon after the while ().
  43. +
  44. +/**James Robinson***********************
  45.  
  46. Well, let's give him a correct answer.
  47.  
  48. In the *original* loop, just change the test (x <= XMAX) to (x < XMAX).
  49. There.  That wasn't so hard, was it?
  50.  
  51. -- Doug.
  52.